package com.helloUIBinder.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.helloUIBinder.client.receivers.BlogPostReceiver;
import com.helloUIBinder.client.receivers.SecurityContextHolderReceiver;
import com.helloUIBinder.client.widgets.*;
import com.helloUIBinder.model.BlogPost;

import java.util.List;


public class HelloUIBinder implements EntryPoint {

    public void onModuleLoad() {
        org.fusesource.restygwt.client.Defaults.setDateFormat(null);
        SecurityContextHolderReceiver.getCurrentUserLogin(getSecurityHolderCallback());
//        loadMainPage();// init all main elements and load main page

//        BlogPostReceiver.getAllBlogPosts(getBlogPostCallBack());
//        BlogPostReceiver.getBlogPostById(getBlogPostByIdCallBack(), 1);
    }

    private RequestCallback getSecurityHolderCallback() {
        return new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
                String currentUserLogin = response.getText();
//                if (currentUserLogin.equals("first")) {
//                    loadMainAdminPage();
//                } else {
//                    loadMainUserPage();
//                }
                loadMainAdminPage();
            }

            @Override
            public void onError(Request request, Throwable exception) {
                Window.alert("Error occurred" + exception.getMessage());
            }
        };
    }

    private void loadMainAdminPage() {

        AdminHeaderWidget adminHeaderWidget = new AdminHeaderWidget();
        HomePageWidget homePageWidget = new HomePageWidget();
        BodyContainerWidget bodyContainerWidget = new BodyContainerWidget();
        FooterWidget footerWidget = new FooterWidget();

        RootPanel.get().add(adminHeaderWidget);
        RootPanel.get().add(bodyContainerWidget);
        RootPanel.get().add(footerWidget);

        bodyContainerWidget.getBodyContainer().add(homePageWidget);
//        bodyContainerWidget.getBodyContainer().add(adminBlogPostEditWidget);
    }

    private void loadMainUserPage() {
        HeaderWidget headerWidget = new HeaderWidget();
        HomePageWidget homePageWidget = new HomePageWidget();
        BodyContainerWidget bodyContainerWidget = new BodyContainerWidget();
        FooterWidget footerWidget = new FooterWidget();
        RootPanel.get().add(headerWidget);
        RootPanel.get().add(bodyContainerWidget);
        RootPanel.get().add(footerWidget);

        bodyContainerWidget.getBodyContainer().add(homePageWidget);
    }
}
